package com.example.springmvcdemo.controller;


import com.example.springmvcdemo.model.User;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/response")
public class ResponseController {
    @RequestMapping("/returnHtmlPage")
    public String returnHtmlPage() {
        return "/hello.html";
    }

    @ResponseBody
    @RequestMapping("/returnData")
    public String returnData() {
        return "hahahahahahaha";
    }

    @ResponseBody
    @RequestMapping("/returnHtml")
    public String returnHtml() {
        return "<h1>我是html代码</h1>";
    }

    @ResponseBody
    @RequestMapping("/returnJson")
    public User returnJson() {
        User user=new User();
        user.setName("zhangsan");
        user.setAge(18);
        return user;
    }

    @ResponseBody
    @RequestMapping("/setStatus")
    public User setStatus(HttpServletResponse response) {
        User user=new User();
        user.setName("zhangsan");
        user.setAge(18);
        response.setStatus(500);
        return user;
    }

    @ResponseBody
    @RequestMapping(value="/setHeader",produces="application/json")
    public String setHeader() {
        return "{\"success\":true}";
    }

    @ResponseBody
    @RequestMapping(value="/setHeader2")
    public String setHeader2(HttpServletResponse response) {
        response.setHeader("myKey","myValue");
        return "success";
    }
}
